package com.redis.demo.rabbitmq.Simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.redis.demo.rabbitmq.Constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class producerDemo {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setUsername(Constants.USERNAME);
        factory.setPassword(Constants.PASSWORD);
        //设置虚拟机的地址
        factory.setVirtualHost(Constants.VIRTUALHOST);
        Connection connection=factory.newConnection();
        //开启信道
        Channel channel = connection.createChannel();
        //声明交换机
        //声明队列
        /*
        *Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments) throws IOException;
        *queue 队列名称
        * durable 是否是可持久的
        * exclusive 是否独占信道
        * autoDelete 是否自动删除
        *
        *
        *
        * */
        //String msg="hello rabbitmq";
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
        /*channel.basicPublish("" ,"hello",null,msg.getBytes());*/
        for (int i = 0; i < 10; i++) {
            String msg="hello rabbitmq";
            channel.basicPublish("" ,Constants.WORK_QUEUE,null,msg.getBytes());
        }
        Thread.sleep(2000);
        System.out.println("消息发送成功");
        channel.close();
        connection.close();
    }
}
